<?php
namespace app\massage\controller;
use app\AdminRest;
use app\massage\model\CarAtvRecord;
use app\massage\model\Comment;
use app\massage\model\CommentLable;
use app\massage\model\Lable;
use app\massage\model\NoticeList;
use app\massage\model\Order;

use app\massage\model\Printer;
use app\massage\model\RefundOrder;
use app\massage\model\User;
use app\shop\model\OrderGoods;

use app\shop\model\Wallet;
use think\App;
use app\shop\model\Order as Model;
use think\facade\Db;


class AdminOrder extends AdminRest
{


    protected $model;

    protected $order_goods_model;

    protected $refund_order_model;

    protected $comment_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Order();

        $this->order_goods_model = new OrderGoods();

        $this->refund_order_model = new RefundOrder();

    }





    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:43
     * @功能说明:列表
     */
    public function orderList(){


        $input = $this->_param;
        //超时自动取消订单
//        $this->model->autoCancelOrder($this->_uniacid);

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        //时间搜素
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];
        }
        //商品名字搜索
        if(!empty($input['goods_name'])){

            $dis[] = ['b.goods_name','like','%'.$input['goods_name'].'%'];
        }

        //手机号搜索
        if(!empty($input['mobile'])){

            $dis[] = ['a.mobile','like','%'.$input['mobile'].'%'];
        }

        if(!empty($input['pay_type'])){
            //订单状态搜索
            $dis[] = ['a.pay_type','=',$input['pay_type']];

        }
//        //除开删除的
        $dis[] = ['a.is_show','=',1];


        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];
        }

        $data = $this->model->adminDataList($dis,$input['limit']);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:58
     * @功能说明:订单详情
     */
    public function orderInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $user_model = new User();

        $data['nickName'] = $user_model->where(['id'=>$data['user_id']])->value('nickName');

        return $this->success($data);

    }






    /**
     * @author chenniang
     * @DataTime: 2021-03-19 17:50
     * @功能说明:退款订单详情
     */
    public function refundOrderInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->refund_order_model->dataInfo($dis);

        $data['pay_order_code'] = $this->model->where(['id'=>$data['order_id']])->value('order_code');

        $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

        $data['refund_time'] = date('Y-m-d H:i:s',$data['refund_time']);

        $user_model = new User();

        $data['nickName'] = $user_model->where(['id'=>$data['user_id']])->value('nickName');

        $data['mobile'] = $this->model->where(['id'=>$data['order_id']])->value('mobile');

        return $this->success($data);

    }
    /**
     * @author chenniang
     * @DataTime: 2021-03-17 17:44
     * @功能说明:退款订单列表
     */
    public function refundOrderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        //商品名字搜索
        if(!empty($input['goods_name'])){

            $dis[] = ['c.goods_name','like','%'.$input['goods_name'].'%'];
        }
        //订单状态搜索
        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];

        }else{

            $dis[] = ['a.status','>',-1];

        }

        $where = [];

        if(!empty($input['order_code'])){

            $where[] = ['a.order_code','like','%'.$input['order_code'].'%'];

            $where[] = ['d.order_code','like','%'.$input['order_code'].'%'];
        }

        $data = $this->refund_order_model->adminDataList($dis,$input['limit'],$where);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-18 09:21
     * @功能说明:拒绝退款
     */
    public function noPassRefund(){

        $input = $this->_input;

        $res = $this->refund_order_model->noPassRefund($input['id']);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**\
     * @author chenniang
     * @DataTime: 2021-03-18 09:28
     * @功能说明:同意退款
     */
    public function passRefund(){

        $input = $this->_input;

        $res = $this->refund_order_model->passOrder($input['id'],$input['price'],$this->payConfig(),0,$input['text']);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-05 23:16
     * @功能说明:评价列表
     */
    public function commentList(){

        $input = $this->_param;


        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','>',-1];

        if(!empty($input['star'])){

            $dis[] = ['a.star','=',$input['star']];
        }

        if(!empty($input['coach_name'])){

            $dis[] = ['d.coach_name','like','%'.$input['coach_name'].'%'];
        }

        if(!empty($input['goods_name'])){

            $dis[] = ['c.goods_name','like','%'.$input['goods_name'].'%'];

        }

        $data = $this->comment_model->dataList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-05 23:31
     * @功能说明:编辑评价
     */
    public function commentUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->comment_model->dataUpdate($dis,$input);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:53
     * @功能说明:评价标签列表
     */
    public function commentLableList(){

        $input = $this->_param;

        $lable_model = new Lable();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $data = $lable_model->dataList($dis,$input['limit']);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:56
     * @功能说明:添加评价标签
     */
    public function commentLableAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $lable_model = new Lable();

        $res = $lable_model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:57
     * @功能说明:编辑评价标签
     */
    public function commentLableUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $lable_model = new Lable();

        $res = $lable_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-04 18:59
     * @功能说明:评价标签详情
     */
    public function commentLableInfo(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $lable_model = new Lable();

        $res = $lable_model->dataInfo($dis);

        return $this->success($res);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-16 10:27
     * @功能说明:提示列表
     */
    public function noticeList(){

        $input = $this->_param;

        $notice_model = new NoticeList();

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['type'])){

            $dis[] = ['type','=',$input['type']];
        }

        if(isset($input['have_look'])){

            $dis[] = ['have_look','=',$input['have_look']];

        }else{

            $dis[] = ['have_look','>',-1];

        }

        if(!empty($input['start_time'])){

            $dis[] = ['create_time','between',"{$input['start_time']},{$input['end_time']}"];
        }

        $data = $notice_model->dataList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-16 10:38
     * @功能说明:未查看的数量
     */
    public function noLookCount(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['have_look','=',0];

        $notice_model = new NoticeList();

        $data = $notice_model->where($dis)->count();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-16 10:41
     * @功能说明:全部已读
     */
    public function allLook(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['have_look','=',0];

        $notice_model = new NoticeList();

        $data = $notice_model->dataUpdate($dis,['have_look'=>1]);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-16 10:28
     * @功能说明:
     */
    public function noticeUpdate(){

        $input = $this->_input;

        $notice_model = new NoticeList();

        $data = $notice_model->dataUpdate(['id'=>$input['id']],$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-11 16:26
     * @功能说明:订单打印
     */
    public function orderPrinter(){

        $input = $this->_input;

        $printer_model = new Printer();

        $res = $printer_model->printer($input['id'],0,$input['type']);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-12 15:25
     * @功能说明:后台数据统计
     */
    public function dataStatistics(){

        $input = $this->_input;

        $car_order_model = new Order();

        $record_model = new CarAtvRecord();

        $shop_order_model = new \app\shop\model\Order();

        $restaurant_order_model = new \app\restaurant\model\Order();

        $dis[] =['pay_type','>',1];

        $dis[] =['uniacid','=',$this->_uniacid];

        $car_order_price = $car_order_model->where($dis)->sum('pay_price');

        $record_price    = $record_model->where($dis)->sum('pay_price');

        $data['car_price'] = round($car_order_price+$record_price,2);

        $shop_price       = $shop_order_model->where($dis)->sum('true_price');

        $restaurant_price = $restaurant_order_model->where($dis)->sum('true_price');

        $data['shop_price'] = round($shop_price,2);

        $data['restaurant_price'] = round($restaurant_price,2);

        $data['all_price'] = round($car_order_price+$record_price+$shop_price+$restaurant_price,2);

        $arr['price'] = $data;

        while ($input['date_start_time']<=$input['date_end_time']){

            $date[] = date('Y-m-d',$input['date_start_time']);

            $input['date_start_time'] +=86400;

        }

        if(!empty($date)){

            foreach ($date as $k=> $value){

                $dis = [];

                $dis[] =['pay_type','>',1];

                $dis[] =['uniacid','=',$this->_uniacid];

                $car_order_price = $car_order_model->where($dis)->whereDay('pay_time',$value)->sum('pay_price');

                $record_price    = $record_model->where($dis)->whereDay('pay_time',$value)->sum('pay_price');

                $arr['date'][$k]['car_price']= round($car_order_price+$record_price,2);

                $shop_price       = $shop_order_model->where($dis)->whereDay('pay_time',$value)->sum('true_price');

                $restaurant_price = $restaurant_order_model->where($dis)->whereDay('pay_time',$value)->sum('true_price');

                $arr['date'][$k]['shop_price'] = round($shop_price,2);

                $arr['date'][$k]['restaurant_price'] = round($restaurant_price,2);

                $arr['date'][$k]['all_price'] = round($car_order_price+$record_price+$shop_price+$restaurant_price,2);

                $arr['date'][$k]['date'] = $value;

            }

        }

        $arr['date'] = array_values($arr['date']);

        $dis[] =['pay_type','>',1];

        $dis[] =['uniacid','=',$this->_uniacid];

        if(!empty($input['start_time'])){

            $dis[] =['pay_time','between',"{$input['start_time']},{$input['end_time']}"];

        }

        if($input['type']==1){

            $arr['order_list'] = $car_order_model->where($dis)->field('id,order_code,pay_price,pay_time')->order('id desc')->paginate($input['limit'])->toArray();

        }elseif($input['type']==2){

            $arr['order_list'] = $shop_order_model->where($dis)->field('id,order_code,true_price as pay_price,pay_time')->order('id desc')->paginate($input['limit'])->toArray();

        }else{

            $arr['order_list'] = $restaurant_order_model->where($dis)->field('id,order_code,true_price as pay_price,pay_time')->order('id desc')->paginate($input['limit'])->toArray();

        }

        return $this->success($arr);

    }


}
